import time
import network
import machine
import socket


def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('zzl', 'zhaozilu121')
        i = 1
        while not wlan.isconnected():
            print("正在连接...{}".format(i))
            i += 1
            time.sleep(1)
    print('network config:', wlan.ifconfig())


do_connect()

server_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_s.bind(("", 8080))

server_s.listen(128)

print("等待对方连接...")

new_s, client_info = server_s.accept()

print("等待对方发送图片...")

for i in range(1, 11):
    with open("img{}.dat".format(i), "wb") as f:
        for i in range(240):
            data = new_s.recv(480)
            f.write(data)

    print("接收完毕{}".format(i))

new_s.close()
server_s.close()